package com.example.hubu20250620.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hubu20250620.entity.User;
import com.example.hubu20250620.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService  extends ServiceImpl<UserMapper, User> {
    public User getUserByUserName(User user1) {
        //根据用户名查询用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, user1.getUserName());
        User user2 = this.getOne(queryWrapper);
        if (user2 != null) {
            if (user2.getPassWord().equals(user1.getPassWord())) {
                return user2;
            } else {
                return null;
            }
        } else {
            return null;
        }

    }
    public Integer registry (User user){
        //根据用户名去重
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, user.getUserName());
        User user1 = this.getOne(queryWrapper);
        if (user1==null){
            if (this.save(user)){
                return 1;//注册成功
            }else{
                return -1;//注册失败
            }
        }else{
            return 0;//用户已经存在
        }

    }
}
